package cn.com.lsy.product.lepao.util;

import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class DozerMapperUtil {

    static Mapper mapper = new DozerBeanMapper();

    /**
     * 将list转换为vo的list
     *
     * @param source
     * @param destType
     * @return
     */
    public static <T, U> List<U> mapList(final List<T> source, final Class<U> destType) {
        final List<U> dest = new ArrayList<U>();

        if (source != null && source.size() > 0) {

            for (T element : source) {
                dest.add(mapper.map(element, destType));
            }
        }

        return dest;
    }

    //将Collection<E>
    public static <T, U> Collection<U> mapCollection(final Collection<T> source, final Class<U> destType) {
        final Collection<U> dest = new ArrayList<>();
        for (T element : source) {
            dest.add(mapper.map(element, destType));
        }
        return dest;
    }

    /**
     * 转换单个vo对象,自动生成目标实例化对象
     *
     * @param source
     * @param destType
     * @return
     */
    public static <U> U map(final Object source, final Class<U> destType) throws IllegalAccessException, InstantiationException {

        if (source != null) {

            return mapper.map(source, destType);
        }

        return destType.newInstance();
    }


}
